package se_dsp_adv_creative

import (
	"fmt"
	"github.com/gabriel-vasile/mimetype"
	"github.com/gin-gonic/gin"
	"io"
	"net/http"
	"os"
	"path/filepath"
	"time"
)

func UploadFileVideo(c *gin.Context) {
	UploadFileVideoOss(c)
	return
	// 限制上传大小
	errr := c.Request.ParseMultipartForm(50 << 20) // 50MB
	if errr != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "file limit 10M :" + errr.Error(),
			"data": nil,
		})
		return
	}

	file, header, err := c.Request.FormFile("file")
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "file upload error1:" + err.Error(),
			"data": nil,
		})
		return
	}

	buffer := make([]byte, 512)
	if _, err = file.Read(buffer); err != nil && err != io.EOF {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "file upload error2 :" + errr.Error(),
			"data": nil,
		})
		return
	}

	mtype := mimetype.Detect(buffer)
	if !mtype.Is("video/mp4") {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "file upload type error",
			"data": nil,
		})
		return
	}

	defer file.Close()
	//上传文件
	uploadDir := "./upload_crt"
	// 生成唯一文件名
	oldname := header.Filename
	ext := filepath.Ext(header.Filename)
	newFilename := fmt.Sprintf("%d%s", time.Now().UnixNano(), ext)
	filePath := filepath.Join(uploadDir, newFilename)
	// 保存文件
	out, err := os.Create(filePath)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "file upload error3:" + err.Error(),
			"data": nil,
		})
		return
	}
	defer out.Close()

	_, err = io.Copy(out, file)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "file upload error4:" + err.Error(),
			"data": nil,
		})
		return
	}

	fileInfo, _ := os.Stat(filePath)
	sizeKB := float64(fileInfo.Size()) / 1024

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": map[string]interface{}{
			"adm_type": "2",
			"ad_name":  oldname,
			"ad_file":  newFilename,
			"desc":     "",
			"width":    0,
			"height":   0,
			"duration": 0,
			"size":     sizeKB,
			"cimg":     "",
			"ad_urls":  "http://hxdsp.huayanz.com/prct/" + newFilename,
		},
	})
	return
}

func UploadFileImg(c *gin.Context) {
	UploadFileImgOss(c)
	return
	// 限制上传大小
	errr := c.Request.ParseMultipartForm(20 << 20) // 20MB
	if errr != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "file limit 10M :" + errr.Error(),
			"data": nil,
		})
		return
	}

	file, header, err := c.Request.FormFile("file")
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "file upload error1:" + err.Error(),
			"data": nil,
		})
		return
	}
	defer file.Close()
	//上传文件
	uploadDir := "./upload_crt"
	// 生成唯一文件名
	oldname := header.Filename
	ext := filepath.Ext(header.Filename)
	newFilename := fmt.Sprintf("%d%s", time.Now().UnixNano(), ext)
	filePath := filepath.Join(uploadDir, newFilename)
	// 保存文件
	out, err := os.Create(filePath)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "file upload error3:" + err.Error(),
			"data": nil,
		})
		return
	}
	defer out.Close()

	_, err = io.Copy(out, file)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "file upload error4:" + err.Error(),
			"data": nil,
		})
		return
	}

	//buffer := make([]byte, 512)
	//if _, err = file.Read(buffer); err != nil && err != io.EOF {
	//	c.JSON(http.StatusOK, gin.H{
	//		"code": -1,
	//		"msg":  "file upload error2 :" + errr.Error(),
	//		"data": nil,
	//	})
	//	return
	//}

	//mtype := mimetype.Detect(buffer)
	mtype, err := mimetype.DetectFile(filePath)
	if err != nil && err != io.EOF {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "file upload error2 :" + errr.Error(),
			"data": nil,
		})
		return
	}
	if !mtype.Is("image/webp") && !mtype.Is("image/jpeg") && !mtype.Is("image/png") && !mtype.Is("image/jpg") {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "file upload type error",
			"data": nil,
		})
		return
	}

	fileInfo, _ := os.Stat(filePath)
	sizeKB := float64(fileInfo.Size()) / 1024

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "success",
		"data": map[string]interface{}{
			"adm_type": "1",
			"ad_name":  oldname,
			"ad_file":  newFilename,
			"desc":     "",
			"width":    0,
			"height":   0,
			"duration": 0,
			"size":     sizeKB,
			"cimg":     "",
			"ad_urls":  "http://hxdsp.huayanz.com/prct/" + newFilename,
		},
	})
	return
}
